﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;

namespace Hangout.Cloud.Agent.Agents
{
    class CompatibilityAgent
    {
        public void Run()
        {
            //check compatibility everyday :)
            while (true)
            {
                //get all the users with outdated compatibility
                Web.Model.HangoutDBEntities context = new Web.Model.HangoutDBEntities();
                try
                {
                    Trace.WriteLine("CA Entered");

                    List<int> ids = Web.Core.BackgroundData.UserBackgroundData.GetOutDatedCompatibilityScoreUserIds(new TimeSpan(1, 0, 0, 0), ref context);

                    Trace.WriteLine("COUNT = " + ids.Count);

                    foreach (int id in ids)
                    {
                        Trace.WriteLine("Ids = " + id);
                        UpdateCompatibility(id, ref context);
                    }
                }
                
                catch(Exception ex)
                {
                    Web.Core.Exceptions.ExceptionReporting.AddAnException(0,Web.Core.ClientType.WindowsAzure,ex,ref context);
                }
               

                Thread.Sleep(new TimeSpan(1, 0, 0, 0)); //sleep for a day :)
            }
        }

        public static void UpdateCompatibility(int id,ref Web.Model.HangoutDBEntities context)
        {
            Web.Core.Compatibility.Compatibility.UpdateCompatibility(id, ref context);
            Web.Core.BackgroundData.UserBackgroundData.UpdateCompatibilityDateTimeStamp(id, ref context);
        }

        
    }
}
